const app = getApp();
Page({
  data: {
    server:app.globalData.server,
    api:app.globalData.api,
    token:'',
    addressDetail:{},
    addOrEdit:0,//新增是0，修改地址是1
    confirmToAddress:null
  },
  onLoad(query) {
    console.log(query)
    let that = this;
    my.getStorage({
      key: 'userInfo',
      success: function(res) {
        that.setData({
          token:res.data?res.data.token:''
        })
        if(query.index!="undefined"){
          //  用index去调改地址详情接口
          that.getInitAddress(query.index)
          that.setData({
            addOrEdit:1
          })
        }else{
          that.setData({
            addOrEdit:0,
            addressDetail:{}
          })
        }
      }
    });
    if(query.confirmToAddress){
      this.setData({
        confirmToAddress:query.confirmToAddress
      })
    }
    console.log(this.data.confirmToAddress)
  },
  onUnload() {
    this.setData({
      confirmToAddress:null
    })
  },
  getInitAddress(id){
    let that = this;
    my.request({
      url:this.data.api+"/user/addressItem?id="+id,
      method: 'get',
      headers:{
        'content-type':'application/json',
        'token':this.data.token
      },
      dataType: 'json',
      success: function(res) {
        console.log(res)
        if(res.data.errCode==401){//token过期了
          my.removeStorage({
            key: 'userInfo'
          });
          that.setData({
            token:'',
          })
          my.showToast({
            type: 'exception',
            content: res.data.message,
            duration: 2000
          });
          my.reLaunch({ url: '../login/login' });
          return false
        }
        if(res.data.errCode==0){
          that.setData({
            addressDetail:res.data.result
          })
        }
      }
    });
  },


  getName(e) {
   this.data.addressDetail.receiver_name=e.detail.value
  },
  getPhone(e) {
   this.data.addressDetail.phone=e.detail.value
  },
  getAddress(e) {
   this.data.addressDetail.address=e.detail.value
   console.log(this.data.addressDetail.address)
  },
  switchDefault(){
    this.data.addressDetail.status=this.data.addressDetail.status?0:1;
    console.log(this.data.addressDetail.status)
  },

  
  bindSaveAddress(){
    console.log(this.data.addressDetail)
    //校验手机号码
    if(!this.data.addressDetail.receiver_name){
      my.showToast({
        type: 'fail',
        content: '接收人不能为空！',
        duration: 2000
      }); 
      return false; 
    }
    if(!(/^1[3456789]\d{9}$/.test(this.data.addressDetail.phone))){ 
      my.showToast({
        type: 'fail',
        content: '手机号码有误，请重填',
        duration: 2000
      }); 
      return false; 
    }
    if(!this.data.addressDetail.address){
      my.showToast({
        type: 'fail',
        content: '详细地址不能为空！',
        duration: 2000
      }); 
      return false; 
    }
    let url;
    console.log(this.data.addOrEdit)
    if(this.data.addOrEdit==0){//新增
      url = "/user/addAddress"
    }else{
      url = "/user/updateAddress"
    }
    let addressInfo = this.data.addressDetail;
    if(addressInfo.status==undefined){
      addressInfo.status = 0;
    }
    console.log(addressInfo)
    let that = this;
    my.request({
      url:this.data.api+url,
      method: 'post',
      headers:{
        'content-type':'application/json',
        'token':this.data.token
      },
      dataType: 'json',
      data: {
        addressInfo:addressInfo
      },
      success: function(res) {
        console.log(res)
        if(res.data.errCode==401){//token过期了
          my.removeStorage({
            key: 'userInfo'
          });
          that.setData({
            token:'',
          })
          my.showToast({
            type: 'exception',
            content: res.data.message,
            duration: 2000
          });
          my.reLaunch({ url: '../login/login' });
          return false
        }
        if(res.data.errCode==0){
          my.showToast({
            content: res.data.message,
            type: 'success',
            duration: 2000,
            success:function(){
              if(that.data.confirmToAddress){
                my.redirectTo({ url: '../address/address?confirmToAddress=1' })
                that.setData({
                  confirmToAddress:null
                })
              }else{
                my.redirectTo({ url: '../address/address' })
              }
            }
          });
        }
      }
    });
  }
});
